package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;

import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.vo.DishVO;
import com.itheima.reggie.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;


    //--1---   【分页】  ___请求url地址：/setmeal/page ?page=1&pageSize=10&name=黑马
    @GetMapping("/page")
    public R<Page<SetmealVO>> page(Long page, Long pageSize, String name){

        //--1.1 分页器----得到 List<Setmeal>
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        setmealService.page(setmealPage,
                new LambdaQueryWrapper<Setmeal>()
                        .like(Objects.nonNull(name),Setmeal::getName,name).orderByDesc(Setmeal::getUpdateTime));

        List<Setmeal> setmealPageRecords = setmealPage.getRecords();

        //--1.2 装填属性---
        List<SetmealVO> setmealVOList=setmealPageRecords.stream().map(item->{
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(item,setmealVO);

            //--T1 装填属性:"setmealDishes": null
            //  setmeal:id ---> 查setmealDish:setmeal_id
            List<SetmealDish> setmealDishList = setmealDishService.list(
                    new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, item.getId()));
            setmealVO.setSetmealDishes(setmealDishList);

            //--T2 装填属性:"categoryName": "儿童套餐"
            //  setmeal:category_id ---> 查category:name
            Category category = categoryService.getOne(
                    new LambdaQueryWrapper<Category>().eq(Category::getId, item.getCategoryId()));
            setmealVO.setCategoryName(category.getName());

            return setmealVO;
        }).collect(Collectors.toList());

        //--1.3 List<Setmeal> 封装List<SetmealVO>
        Page<SetmealVO> setmealVOPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,setmealVOPage,"records");
        setmealVOPage.setRecords(setmealVOList);
        log.info("[info--setmealVOPage]: {}",setmealVOPage);

        return R.success(setmealVOPage);
    }

    //--2---   【添加】
    //请求url地址：/setmeal
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
    public R saveSetmeal(@RequestBody SetmealVO setmealVO){
        setmealService.saveWithsetmealDish(setmealVO);
        return R.success("保存套餐成功!!");
    }

    //--3---   【删除】
    //请求url地址：/setmeal ?ids=1415580119015145474,1505375213336915969
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
    public R<String> deleteSetmeal(@RequestParam List<Long> ids){
        log.info("[>>>删除套餐--deleteID]: {}",ids);
        return R.success(setmealService.deleteWithSetmealDish(ids));
    }


    //--4---   【起售、停售】请求url地址：/setmeal/status/1
    @PostMapping("/status/{status}")
    public R<String> upSetmeal(@RequestParam List<Long> ids,@PathVariable Integer status){
        log.info("[>>>起售/停售套餐--upSetmeal]: ids={},status={}",ids,status);
        setmealService.upSetmeal(ids,status);
        return R.success("操作成功");
    }


    //--3-- 修改---传id，回显数据
    @GetMapping("/{id}")
    public R<SetmealVO>  selectById(@PathVariable Long id){
        log.info("[>>>修改菜品--传id回显数据]:{}",id);

        //源对象拷贝
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmealService.getById(id),setmealVO);

        List<SetmealDish> list = setmealDishService.list(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, id));
        setmealVO.setSetmealDishes(list);
        return R.success(setmealVO);
    }
    //--5---   【修改】请求url地址：/setmeal/1415580119015145474
    @PutMapping()
    public R update(@RequestBody SetmealVO setmealVO){
        log.info("[>>>修改菜品--SetmealVO]:{}",setmealVO);
        setmealService.updateWithsetmealDish(setmealVO);
        return R.success("修改套餐成功");
    }


    //-M1-【客户端】根据套餐分类id查询该套餐分类下的套餐
    //请求url地址：/setmeal/list ?categoryId=1413342269393674242&status=1
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R<List<Setmeal>> SesetmealLsitById(Setmeal setmeal){
        Long categoryId=setmeal.getCategoryId();
        Integer status=setmeal.getStatus();
        log.info("[>>>--<套餐模块>套餐分类id]: categoryId={},status={}",categoryId,status);

        List<Setmeal> list = setmealService.list(
                new LambdaQueryWrapper<Setmeal>()
                        .eq(Setmeal::getCategoryId, categoryId).eq(Setmeal::getStatus, status));
        return R.success(list);
    }

}
